import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { IView } from "../../mvc/interfaces/IView";
import { ShopModule } from "../../../modules/ShopModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SContriShopBuyRecord } from "../../../Message/Public/SContriShopBuyRecord";
import { SMoney } from "../../../Message/Game/SMoney";
import { ShopItemInfo } from "../resource/info/ShopItemInfo";
import { ItemData } from "../resource/info/ItemData";
import { TItemWeapon } from "../../../Message/Db/Tables/TItemWeapon";
import { ECategory } from "../../../Message/Public/ECategory";
import { ERepairType } from "../../../Message/Game/ERepairType";
import { EPetEquipRepairType } from "../../../Message/Game/EPetEquipRepairType";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundTypeConst } from "../../common/sound/SoundTypeConst";
import { SPlayerItem } from "../../../Message/Public/SPlayerItem";
import { Global } from "../../../com/gengine/global/Global";
import { GameController } from "../mvc/GameController";
import { NormalShopType } from "../view/shop/info/NormalShopType";
import { LayerManager } from "../manager/LayerManager";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { MeritShopModule } from "../../../modules/MeritShopModule";
import { HonorShopModule } from "../../../modules/HonorShopModule";
import { EPrictUnit } from "../../../Message/Public/EPrictUnit";
import { ShopConfig } from "../resource/ShopConfig";
import { ConstConfig } from "../resource/ConstConfig";
import { GangFightShopBuyLimitInfo } from "../view/shop/info/GangFightShopBuyLimitInfo";
import { ArenaShopBuyLimitInfo } from "../view/shop/info/ArenaShopBuyLimitInfo";
import { IShopModule } from "../../../modules/interfaces/IShopModule";
import { IMeritShopModule } from "../../../modules/interfaces/IMeritShopModule";
import { IHonorShopModule } from "../../../modules/interfaces/IHonorShopModule";
import { DrugShop } from "../../../NormalShopType/DrugShop";
type int = number;
//class ShopController
    
    export  class ShopController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.ShopOpen, this.onShopOpenHandler);
            Dispatcher.addEventListener(EventName.ShopClose, this.onShopCloseHandler);
            Dispatcher.addEventListener(EventName.ShopOpenHonor, this.onShopOpenHonorHandler);
            Dispatcher.addEventListener(EventName.VIPShopOpen, this.onVIPShopOpenHandler);
            Dispatcher.addEventListener(EventName.ShopOpenMerit, this.onShopOpenMeritHandler);
            Dispatcher.addEventListener(EventName.ShopBuyItem, this.buyItemHandler);
            Dispatcher.addEventListener(EventName.ShopSellItem, this.sellItemHandler);
            Dispatcher.addEventListener(EventName.ShopSellItemSuccess, this.ShopSellItemSuccessHandler);
            Dispatcher.addEventListener(EventName.ShopFixEquipment, this.fixEquipmentHandler);
            Dispatcher.addEventListener(EventName.ShopFixAllEquipment, this.fixAllEquipmentHandler);
            Dispatcher.addEventListener(EventName.ShopFixEquipmentSuccess, this.fixEquipmentSuccessHandler);
            Dispatcher.addEventListener(EventName.ShopFixAllEquipmentSuccess, this.fixAllEquipmentSuccessHandler);
            Dispatcher.addEventListener(EventName.ShopSellList, this.onSellListHandler);
            Dispatcher.addEventListener(EventName.ShopBuyBack, this.onBuyBackHandler);
            Dispatcher.addEventListener(EventName.RepairAllWeaponCost, this.onRepairAllWeaponCost);
            Dispatcher.addEventListener(EventName.ShopWrestleBuyRecord, this.onShopWrestleBuyRecordHandler);
            Dispatcher.addEventListener(EventName.ShopContriBuyRecord, this.onShopContriBuyRecord);
            Dispatcher.addEventListener(EventName.GangFightBuyRecord, this.onGangFightBuyRecord);
            return;
        }

        /* internal  */onShopContriBuyRecord(arg1: DataEvent): void
        {
            GameProxy.shopProxy.getContriShopBuyRecord();
            return;
        }

        /* internal  */onContriShopBuyRecordCommand(arg1: Object): void
        {
            if (this._honorShopModule) 
            {
                this._honorShopModule.refresh();
            }
            return;
        }

        /* internal  */onGangFightShopBuyRecordCommand(arg1: Object): void
        {
            if (this._honorShopModule) 
            {
                this._honorShopModule.refresh();
            }
            return;
        }

        /* internal  */onGangFightBuyRecord(arg1: DataEvent): void
        {
            GameProxy.shopProxy.getGangFightsBuyRecord();
            return;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (null == this._shopModule) 
            {
                this._shopModule = new ShopModule();
                this._shopModule.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
            }
            return this._shopModule;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this._shopModule.defaultSelectBuyBtn();
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.WrestleShopBuyRecordCommand, this.onWrestleShopBuyRecordCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.ContriShopBuyRecordCommand, this.onContriShopBuyRecordCommand);
            NetDispatcher.addCmdListener(ServerCommand.GangFightShopBuyRecordCommand, this.onGangFightShopBuyRecordCommand);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicInterHonourShopBuyRecordEx, this.onHonorRecorHandler);
            return;
        }

        /* internal  */onHonorRecorHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SContriShopBuyRecord;
            cache.shop.honorShopBuyLimitInfo.buyNumDict = loc1.record;
            if (this._honorShopModule) 
            {
                this._honorShopModule.refresh();
            }
            return;
        }

        /* internal  */onMoneyUpdateHandler(arg1: SMoney=null): void
        {
            if (this._shopModule && this._shopModule.isHide == false) 
            {
                this._shopModule.update(cache.role.money);
            }
            return;
        }

        /* internal  */buyItemHandler(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=cache.dialog.npcInfo;
            if (loc2 != null) 
            {
                loc1 = loc2.tnpc.npcId;
            }
            else 
            {
                loc1 = 0;
            }
            var loc3=arg1.data["itemInfo"] as ShopItemInfo;
            var loc4=arg1.data["amount"] as int;
            var loc5=cache.shop.priorityFlag;
            if (2040 == loc3.tshopSell.shopCode) 
            {
                loc1 = 5000096;
            }
            GameProxy.shopProxy.buyItem(loc1, loc3.tshopSell.shopCode, loc3.itemInfo.item.code, loc4, null, loc5);
            return;
        }

        /* internal  */sellItemHandler(arg1: DataEvent): void
        {
            var loc1=0;
            if (!cache.vip.isVIP) 
            {
                if (cache.dialog.npcInfo) 
                {
                    loc1 = cache.dialog.npcInfo.tnpc.npcId;
                }
                else 
                {
                    loc1 = 11;
                }
            }
            var loc2=[];
            var loc3=arg1.data as string;
            loc2.push(loc3);
            GameProxy.shopProxy.sellItem(loc1, loc2);
            return;
        }

        /* internal  */ShopSellItemSuccessHandler(arg1: DataEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopSellList));
            return;
        }

        /* internal  */fixEquipmentHandler(arg1: DataEvent): void
        {
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=null;
            var loc1=arg1.data["posType"] as int;
            var loc2=arg1.data["itemData"] as ItemData;
            if (loc2) 
            {
                loc3 = loc2.itemInfo;
                loc4 = loc2.itemExInfo;
                if (loc3 && loc3.extend && loc3.extend is TItemWeapon) 
                {
                    loc5 = loc3.extend as TItemWeapon;
                }
                if (loc4) 
                {
                    if (!(loc4.durable == loc5.durableUpper) || !(loc5.durable == loc5.durableUpper)) 
                    {
                        if (loc2.category != ECategory._ECategoryPetEquip) 
                        {
                            if ((loc7 = cache.dialog.npcInfo) != null) 
                            {
                                loc6 = loc7.tnpc.npcId;
                            }
                            else 
                            {
                                loc6 = 0;
                            }
                            GameProxy.shopProxy.repairWeapon(ERepairType.convert(ERepairType._ERepairTypeSingle), loc1, loc2.uid, loc6);
                        }
                        else 
                        {
                            GameProxy.petEquip.repairPetWeapon(EPetEquipRepairType.convert(EPetEquipRepairType._EPetEquipRepairTypeSingle), loc1, loc2.uid);
                        }
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(53058));
                        return;
                    }
                }
                SoundManager.instance.soundPlay(SoundTypeConst.EquipmentRepair);
            }
            return;
        }

        /* internal  */fixEquipmentSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(53059));
            return;
        }

        /* internal  */fixAllEquipmentSuccessHandler(arg1: DataEvent): void
        {
            MsgManager.showRollTipsMsg(Language.getString(53060));
            return;
        }

        /* internal  */fixAllEquipmentHandler(arg1: DataEvent): void
        {
            var loc1=0;
            if (cache.role.money.coin + cache.role.money.coinBind < cache.shop.repairAllCost) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, cache.shop.repairAllCost));
                MsgManager.showRollTipsMsg(Language.getString(53061));
                return;
            }
            if (cache.dialog.npcInfo != null) 
            {
                loc1 = cache.dialog.npcInfo.tnpc.npcId;
            }
            else 
            {
                loc1 = 0;
            }
            GameProxy.shopProxy.repairWeapon(ERepairType.convert(ERepairType._ERepairTypeAllIncludePet), 0, "", loc1);
            SoundManager.instance.soundPlay(SoundTypeConst.EquipmentRepair);
            return;
        }

        /* internal  */onSellListHandler(arg1: DataEvent): void
        {
            GameProxy.shopProxy.sellList();
            return;
        }

        /* internal  */onBuyBackHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPlayerItem;
            GameProxy.shopProxy.buyBack([loc1.uid]);
            return;
        }

        /* internal  */onShopOpenHandler(arg1: DataEvent): void
        {
            var loc4=0;
            var loc1=(Global.stage.stageHeight - GameController.pack.view.height) / 2;
            var loc2=arg1.data["shopCode"] as int;
            var loc3=arg1.data["shopType"] as int;
            (this.view as ShopModule).updateShopTabData(loc3, loc2);
            if (loc3 != NormalShopType.ExchangeShop) 
            {
                if (loc3 == NormalShopType.LoveTileShop) 
                {
                    loc4 = cache.shop.getLoveTileShopTabIndex();
                }
            }
            else 
            {
                loc4 = cache.shop.getDefExchangeShopTabIndex();
            }
            this._shopModule.showDefaultTab(loc4);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopSellList));
            LayerManager.windowLayer.centerWindows(this.view, GameController.pack.view);
            cache.shop.isOpening = true;
            this._shopModule.guideCheck();
            NpcEffectRule.registDialogEffectWindow(this.view);
            NpcEffectRule.registDialogEffectWindow(GameController.pack.view);
            return;
        }

        /* internal  */onVIPShopOpenHandler(arg1: DataEvent): void
        {
            var loc1=1000;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpen, {"shopCode": loc1, "shopType": DrugShop}));
            cache.shop.isVipShopOpening = true;
            return;
        }

        /* internal  */onShopOpenMeritHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            if (!this._meritShopModule) 
            {
                this._meritShopModule = new MeritShopModule();
            }
            this._meritShopModule.setDefaultPage(loc1);
            this._meritShopModule.show();
            return;
        }

        /* internal  */onShopOpenHonorHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["shopCode"] as int;
            var loc2=arg1.data["type"] as int;
            if (this._honorShopModule == null) 
            {
                this._honorShopModule = new HonorShopModule();
            }
            this._honorShopModule.setSelectedBtn();
            this._honorShopModule.setDefaultPage(loc1, loc2);
            var loc3=cache.shop.getDefExchangeShopTabIndex();
            this._honorShopModule.showDefaultTab(loc3);
            LayerManager.windowLayer.centerWindows(this._honorShopModule, GameController.pack.view, 20);
            NpcEffectRule.registDialogEffectWindow(this._honorShopModule);
            NpcEffectRule.registDialogEffectWindow(GameController.pack.view);
            NetDispatcher.dispatchCmd(ServerCommand.MoneyUpdate, cache.role.money);
            this.dealSpecialShopLimit(loc1, loc2);
            return;
        }

        /* internal  */dealSpecialShopLimit(arg1: int, arg2: int): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=0;
            var loc7=null;
            var loc8=null;
            var loc9=null;
            var loc10=null;
            var loc11=0;
            var loc12=0;
            var loc13=null;
            var loc14=null;
            var loc15=null;
            var loc16=null;
            var loc17=null;
            var loc18=null;
            var loc19=0;
            var loc20=0;
            var loc21=null;
            var loc22=null;
            var loc23=null;
            var loc24=null;
            var loc25=0;
            var loc26=0;
            if (arg2 != EPrictUnit._EPriceUnitArena) 
            {
                if (arg2 != EPrictUnit._EPriceUnitCountryContribution) 
                {
                    if (arg2 != EPrictUnit._EPriceUnitMagicStone) 
                    {
                        if (arg2 == EPrictUnit._EPriceUnitInterHonour) 
                        {
                            if ((loc21 = ShopConfig.instance.getShopByName(arg1)) && loc21.items.length > 0) 
                            {
                                loc22 = cache.shop.honorShopBuyLimitInfo;
                                loc26 = 0;
                                while (loc26 < loc21.items.length) 
                                {
                                    loc25 = (loc24 = loc21.items[loc26] as ShopItemInfo).itemInfo.item.code;
                                    if (loc23 = ConstConfig.instance.getObjectByName(loc22.getConstName(loc25))) 
                                    {
                                        loc22.updateBuyLimit(loc25, int(loc23.constValue));
                                    }
                                    ++loc26;
                                }
                            }
                        }
                    }
                    else 
                    {
                        if ((loc13 = ShopConfig.instance.getShopByName(arg1)) && loc13.items.length > 0) 
                        {
                            loc14 = cache.shop.gangFightShopBuyLimitInfo;
                            loc20 = 0;
                            while (loc20 < loc13.items.length) 
                            {
                                loc19 = (loc18 = loc13.items[loc20] as ShopItemInfo).itemInfo.item.code;
                                loc15 = ConstConfig.instance.getObjectByName(loc14.getMagicStoneConst(loc19));
                                loc16 = ConstConfig.instance.getObjectByName(loc14.getTimeLimitConst(loc19));
                                loc17 = ConstConfig.instance.getObjectByName(loc14.getPersonalTimeLimitConst(loc19));
                                if (loc15) 
                                {
                                    loc14.updateBuyLimit(loc19, int(loc15.constValue), GangFightShopBuyLimitInfo.KEY_MY_LIMIT);
                                    loc14.updateBuyLimit(loc19, int(loc15.constValueEx), GangFightShopBuyLimitInfo.KEY_TEAM_LIMIT);
                                }
                                if (loc16) 
                                {
                                    loc14.updateBuyLimit(loc19, int(loc16.constValue), GangFightShopBuyLimitInfo.KEY_NUM_LIMIT);
                                }
                                if (loc17) 
                                {
                                    loc14.updateBuyLimit(loc19, int(loc17.constValue), GangFightShopBuyLimitInfo.KEY_NUM_LIMIT);
                                }
                                ++loc20;
                            }
                        }
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GangFightBuyRecord));
                    }
                }
                else 
                {
                    if ((loc7 = ShopConfig.instance.getShopByName(arg1)) && loc7.items.length > 0) 
                    {
                        loc8 = cache.shop.countryContributionShopBuyLimitInfo;
                        loc12 = 0;
                        while (loc12 < loc7.items.length) 
                        {
                            loc11 = (loc10 = loc7.items[loc12] as ShopItemInfo).itemInfo.item.code;
                            if (loc9 = ConstConfig.instance.getObjectByName(loc8.getConstName(loc11))) 
                            {
                                loc8.updateBuyLimit(loc11, int(loc9.constValue));
                            }
                            ++loc12;
                        }
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopContriBuyRecord));
                }
            }
            else 
            {
                loc1 = ShopConfig.instance.getShopByName(arg1);
                if (loc1 && loc1.items.length > 0) 
                {
                    loc2 = cache.shop.arenaShopBuyLimitInfo;
                    loc6 = 0;
                    while (loc6 < loc1.items.length) 
                    {
                        loc5 = (loc4 = loc1.items[loc6] as ShopItemInfo).itemInfo.item.code;
                        if (loc3 = ConstConfig.instance.getObjectByName(loc2.getConstName(loc5))) 
                        {
                            loc2.updateBuyLimit(loc5, int(loc3.constValue), ArenaShopBuyLimitInfo.KEY_ARENA_LIMIT);
                            loc2.updateBuyLimit(loc5, int(loc3.constValueEx), ArenaShopBuyLimitInfo.KEY_NUM_LIMIT);
                        }
                        ++loc6;
                    }
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopWrestleBuyRecord));
            }
            return;
        }

        /* internal  */onShopCloseHandler(arg1: DataEvent): void
        {
            cache.shop.isOpening = false;
            cache.shop.isVipShopOpening = false;
            cache.shop.operType = "";
            return;
        }

        /* internal  */onRepairAllWeaponCost(arg1: DataEvent): void
        {
            GameProxy.shopProxy.repairAllWeaponCost();
            return;
        }

        /* internal  */onShopWrestleBuyRecordHandler(arg1: DataEvent): void
        {
            GameProxy.shopProxy.getWrestleBuyRecord();
            return;
        }

        /* internal  */onWrestleShopBuyRecordCommandHandler(arg1: ArenaShopBuyLimitInfo): void
        {
            if (this._honorShopModule) 
            {
                this._honorShopModule.refresh();
            }
            return;
        }

        private /* var */_shopModule: IShopModule;

        private /* var */_meritShopModule: IMeritShopModule;

        private /* var */_honorShopModule: IHonorShopModule;

        private /* var */_shopCode: int=0;
    }
